{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b144c684",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为__空集______."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbb67025",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为__[30, 4]______."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b542335f",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为___3_____."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d2779e9",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为__{'a;None, 'A' :65}______."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0745597",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(  错 )`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b93510d2",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`( 对   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10e6d58c",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`( 错   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eee10686",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`( 错   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e244a97",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e28e1946",
   "metadata": {},
   "source": [
    "def frequency():\n",
    "    '''\n",
    "    输入一个字符串,输出其中每个字符的出现次数\n",
    "    :return:\n",
    "    '''\n",
    "    from collections import Counter\n",
    "    text = input('请输入一个字符串：')\n",
    "    frequencies = Counter(text)\n",
    "    print(frequencies)\n",
    " \n",
    "frequency()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2398fa15",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3dd86d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = input(\"请输入一个字符串:\")\n",
    "\n",
    "new_str = set(x)\n",
    "\n",
    "print(new_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba87512",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92afa96e",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "222434f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from operator import __or__\n",
    "from functools import reduce\n",
    "y = eval(input('请输入包含若干集合的列表：'))\n",
    "x = reduce(__or__, y)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4721c191",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0413f750",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
